/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import org.hibernate.*;
import pojo.*;
import utils.HibernateUtil;
/**
 *
 * @author DANG
 */
public class LichChieuPhimDAO {
    
    public static SessionFactory _localSessionFactory;

    static {
        _localSessionFactory = (_localSessionFactory != null) ? _localSessionFactory : HibernateUtil.getSessionFactory();
    }
    
     
       
     public static List<Lichchieuphim> getLichChieuPhims() throws Exception{
        List<Lichchieuphim> items = null;
        Session localSession = null;
        String localQuery = "FROM Lichchieuphim l WHERE l.trangThai = :statusLichChieuPhim";
        try{
            //Step 01: creat retrieving session using where clause.
            localSession = _localSessionFactory.openSession();
            //Step 02: begin retrieving transaction in above session.
            localSession.beginTransaction();
            //Step 03: set parameters retrieving objects.
            Query q = localSession.createQuery(localQuery);
            q.setInteger("statusLichChieuPhim", 1);
            items = q.list();
            //Step 04: commit and end retrieving transaction in above session.
            localSession.getTransaction().commit();
        }catch(Exception e){
            throw new Exception(String.format("Failed in %s of %s : %s","getLichChieuPhims",LichChieuPhimDAO.class.getName(),e.getMessage()));
        }
        finally{
            try{
                //Step 05: close retrieving session.
                localSession.close();
                
            }catch(Exception e){
                 throw new Exception(String.format("Failed in %s of %s : %s","getLichChieuPhims",LichChieuPhimDAO.class.getName(),e.getMessage()));
            }
        }
        return items;
    }
    
     public static boolean insertLichChieuPhims(Lichchieuphim paramLichChieuPhims) throws Exception{
        boolean rs = true;
        Session localSession = null;
        try{
            //Step 01: validate input params.
           if(paramLichChieuPhims == null){
                throw new Exception("Param not valid");
            }
            //Step 02: create delete session.
             localSession = _localSessionFactory.openSession();
            //Step 03: begin delete transaction in above session.
             localSession.beginTransaction();
            //Step 04: delete object into database.
             localSession.save(paramLichChieuPhims);
            //Step 05: commit and end delete transaction in above session.
             localSession.getTransaction().commit();
        }catch(Exception e){
            rs = false;
            throw new Exception(String.format("Failed in %s of %s : %s ","insertLichChieuPhims",LichChieuPhimDAO.class.getName(),e.getMessage()));
        }
        finally{
            try{
                //Step 06: close delete session.
                localSession.close();
            }catch(Exception e){
                throw new Exception(String.format("Failed in %s of %s : %s ","insertLichChieuPhims",LichChieuPhimDAO.class.getName(),e.getMessage()));
            }
        }
        return rs;
    }
     
     public static boolean updateLichChieuPhims(Lichchieuphim paramLichChieuPhims) throws Exception{
        boolean rs = true;
        Session localSession = null;
        try{
            //Step 01: validate input params.
           if(paramLichChieuPhims == null){
                throw new Exception("Param not valid");
            }
            //Step 02: create delete session.
             localSession = _localSessionFactory.openSession();
            //Step 03: begin delete transaction in above session.
             localSession.beginTransaction();
            //Step 04: delete object into database.
             localSession.update(paramLichChieuPhims);
            //Step 05: commit and end delete transaction in above session.
             localSession.getTransaction().commit();
        }catch(Exception e){
            rs = false;
            throw new Exception(String.format("Failed in %s of %s : %s ","updateLichChieuPhims",LichChieuPhimDAO.class.getName(),e.getMessage()));
        }
        finally{
            try{
                //Step 06: close delete session.
                localSession.close();
            }catch(Exception e){
                throw new Exception(String.format("Failed in %s of %s : %s ","updateLichChieuPhims",LichChieuPhimDAO.class.getName(),e.getMessage()));
            }
        }
        return rs;
    }
    
     public static boolean deleteLichChieuPhims(Lichchieuphim paramLichChieuPhims) throws Exception{
        boolean rs = true;
        Session localSession = null;
        try{
            //Step 01: validate input params.
           if(paramLichChieuPhims == null){
                throw new Exception("Param not valid");
            }
            //Step 02: create delete session.
             localSession = _localSessionFactory.openSession();
            //Step 03: begin delete transaction in above session.
             localSession.beginTransaction();
            //Step 04: delete object into database.
             localSession.delete(paramLichChieuPhims);
            //Step 05: commit and end delete transaction in above session.
             localSession.getTransaction().commit();
        }catch(Exception e){
            rs = false;
            throw new Exception(String.format("Failed in %s of %s : %s ","deleteLichChieuPhims",LichChieuPhimDAO.class.getName(),e.getMessage()));
        }
        finally{
            try{
                //Step 06: close delete session.
                localSession.close();
            }catch(Exception e){
                throw new Exception(String.format("Failed in %s of %s : %s ","deleteLichChieuPhims",LichChieuPhimDAO.class.getName(),e.getMessage()));
            }
        }
        return rs;
    }
  
}
